/*
 * Chastoslov
 * Copyright (C) 2009 Alexandr Stroghov
 * 
 * stroghov@gmail.com
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License, version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if 
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 * MA 02111-1307 USA
*/
package world.ah.wf.guiclient;
import java.awt.Component;
import java.awt.Frame;
import java.util.Arrays;
import javax.swing.JDialog;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ComponentEvent;
import javax.swing.JToggleButton.ToggleButtonModel;
import javax.swing.JCheckBox;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Dimension;

public class DialogConnProperties extends JDialog {
  private String connectLabel = "Connect";
  private String disconnectLabel = "Disconnect";
  //
  private ConnectionAssistant connAssist;
  public void setConnAssist(ConnectionAssistant connAssist) {
    this.connAssist = connAssist;
  };
  //
  private JButton bClose = new JButton();
  private JLabel labURL = new JLabel();
  private JTextField tfURL = new JTextField();
  private JButton bConnect = new JButton();
  private JTextField tfDriver = new JTextField();
  private JLabel labDriver = new JLabel();
  private JTextField tfLogin = new JTextField();
  private JLabel labLogin = new JLabel();
  private JLabel labPassword = new JLabel();
  private JCheckBox cbRememberPassword = new JCheckBox();
  private JPasswordField tfPassword = new JPasswordField();
  private JLabel labSchema = new JLabel();
  private JTextField tfSchema = new JTextField();
  private JCheckBox cbAutoConnect = new JCheckBox();
  private JButton bSave = new JButton();
  private JPanel pButtons = new JPanel();
  private GridBagLayout gridBagLayout1 = new GridBagLayout();
  private JPanel pCompensator = new JPanel();
  private GridBagLayout gridBagLayout2 = new GridBagLayout();

  public DialogConnProperties() {
    this(null, "", false);
  }

  public DialogConnProperties(Frame parent, String title, boolean modal) {
    super(parent, title, modal);
    try {jbInit();} catch(Exception e) {e.printStackTrace();}
    //
    General.centerFrame(this);
    // Язык (не предполагается переустановка языка во время работы с формой)
    setLang();
    //
    
  }
  // ИНИЦИАЛИЗАЦИЯ
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(gridBagLayout1);
    this.setTitle("Connection parameters");
    this.setResizable(false);
    this.setSize(new Dimension(400, 356));
    bClose.setText("Close");
    labURL.setText("URL");
    labURL.setLabelFor(tfURL);
    bSave.setText("Save");
    bSave.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          bSave_actionPerformed(e);
        }
      });
    pButtons.setLayout(gridBagLayout2);
    pButtons.add(bConnect, new GridBagConstraints(0, 1, 1, 2, 1.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(0, 10, 10, 5), 0, 0));
    pButtons.add(bSave, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(10, 5, 10, 5), 0, 0));
    pButtons.add(bClose, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(10, 5, 10, 10), 0, 0));
    pButtons.add(pCompensator, new GridBagConstraints(0, 0, 3, 1, 0.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(pButtons, new GridBagConstraints(0, 9, 2, 1, 1.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(cbAutoConnect, new GridBagConstraints(0, 8, 2, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(10, 13, 0, 13), 0, 0));
    this.getContentPane().add(tfSchema, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(3, 5, 0, 10), 0, 0));
    this.getContentPane().add(labSchema, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(10, 13, 0, 3), 0, 0));
    this.getContentPane().add(tfPassword, new GridBagConstraints(0, 7, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(3, 10, 0, 5), 0, 0));
    this.getContentPane().add(cbRememberPassword, new GridBagConstraints(1, 6, 1, 2, 0.0, 0.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 10), 0, 0));
    this.getContentPane().add(labPassword, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(10, 13, 0, 3), 0, 0));
    this.getContentPane().add(labLogin, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(10, 13, 0, 3), 0, 0));
    this.getContentPane().add(tfLogin, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(3, 10, 0, 5), 0, 0));
    this.getContentPane().add(labDriver, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(10, 13, 0, 13), 0, 0));
    this.getContentPane().add(tfDriver, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(3, 10, 0, 10), 0, 0));
    this.getContentPane().add(tfURL, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(3, 10, 0, 10), 0, 0));
    this.getContentPane().add(labURL, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(10, 13, 0, 13), 0, 0));
    cbAutoConnect.setText("Connect on start");
    tfSchema.setText("WF");
    labSchema.setText("Schema");
    labSchema.setLabelFor(tfSchema);
    tfLogin.setText("WF");
    labPassword.setLabelFor(tfPassword);
    cbRememberPassword.setText("Remember password");
    labPassword.setText("Password");
    labLogin.setLabelFor(tfLogin);
    labLogin.setText("Login");
    labDriver.setLabelFor(tfDriver);
    labDriver.setText("Driver");
    this.addComponentListener(new java.awt.event.ComponentAdapter() {
        public void componentShown(ComponentEvent e) {
          this_componentShown(e);
        }
      });
    bConnect.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jButton1_actionPerformed(e);
        }
      });
    bConnect.setText(disconnectLabel);
    bClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          bCancel_actionPerformed(e);
        }
      });
    
  }

  private void bCancel_actionPerformed(ActionEvent e) {
    this.setVisible(false);
  }
  
  //
  private void fillConnFromControls() {
    connAssist.setConnDriver(tfDriver.getText());
    connAssist.setConnURL(tfURL.getText());
    connAssist.setSchema(tfSchema.getText());
    connAssist.setUserName(tfLogin.getText());
    char[] pwd = tfPassword.getPassword();
    connAssist.setPassword(pwd);
    Arrays.fill(pwd, '*');
    connAssist.setRememberPassword(cbRememberPassword.isSelected());
    connAssist.setAutoConnect(cbAutoConnect.isSelected());
  }

  private void jButton1_actionPerformed(ActionEvent e) {
    //      
    if(connAssist.getConnected()) 
      try {connAssist.disconnect();} 
      catch (Exception ex) {ErrorShower.show(this, "disconnect", "", ex);}
    else {
      fillConnFromControls();
      try {connAssist.connect();} 
      catch (Exception ex) {ErrorShower.show(this, "connect", "", ex);}
    }
    //
    setControlsByConnection();
  }
  
  private void setControlsByConnection() {
    boolean connected = connAssist.getConnected();
    if(connected) bConnect.setText(disconnectLabel); else bConnect.setText(connectLabel);
    //
    Component[] comps = this.getContentPane().getComponents();
    for(int i = 0; i < comps.length; i++) 
      if(comps[i] != bConnect && comps[i] != bSave && comps[i] != bClose)
        if(comps[i] instanceof JTextField && comps[i] != tfPassword) 
          ((JTextField)comps[i]).setEditable(!connected);
        else comps[i].setEnabled(!connected);
  }
  
  private void this_componentShown(ComponentEvent ev) {
    this.setSize(this.getWidth(), this.getHeight() - pCompensator.getHeight() + 5);
    tfDriver.setText(connAssist.getConnDriver());
    tfURL.setText(connAssist.getConnURL());
    tfSchema.setText(connAssist.getSchema());
    tfLogin.setText(connAssist.getUserName());
    cbRememberPassword.setSelected(connAssist.getRememberPassword());
    char[] pwd = connAssist.getPassword();
    tfPassword.setText(String.valueOf(pwd));
    Arrays.fill(pwd, '*');
    cbAutoConnect.setSelected(connAssist.getAutoConnect());
    setControlsByConnection();
  }

  private void bSave_actionPerformed(ActionEvent e) {
    fillConnFromControls();
    try {connAssist.saveProperties();}
    catch (Exception ex) {ErrorShower.show(this, "save connection parameters", "", ex);}
  }
  /** Устанавливает язык интерфейса */
  public void setLang() {
    String s;
    s = General.getLangItem("ConnectionParameters"); if(s.length() > 0) this.setTitle(s);
    s = General.getLangItem("Connect"); if(s.length() > 0) connectLabel = s;
    s = General.getLangItem("Disconnect"); if(s.length() > 0) disconnectLabel = s;
    s = General.getLangItem("Close"); if(s.length() > 0) bClose.setText(s);
    s = General.getLangItem("URL"); if(s.length() > 0) labURL.setText(s);
    s = General.getLangItem("Save"); if(s.length() > 0) bSave.setText(s);
    s = General.getLangItem("AutoConnect"); if(s.length() > 0) cbAutoConnect.setText(s);
    s = General.getLangItem("Schema"); if(s.length() > 0) labSchema.setText(s);
    s = General.getLangItem("RememberPassword"); if(s.length() > 0) cbRememberPassword.setText(s);
    s = General.getLangItem("Password"); if(s.length() > 0) labPassword.setText(s);
    s = General.getLangItem("Login"); if(s.length() > 0) labLogin.setText(s);
    s = General.getLangItem("Driver"); if(s.length() > 0) labDriver.setText(s);
  }


}
